package com.jwsoft.manager.core.dao.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.IdcardUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.enums.AuthStatusEnum;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.assistant.core.utils.SessionUtil;
import com.bifang.module.base.common.util.CommonSecureUtil;
import com.bifang.module.system.common.enums.CardTypeEnum;
import com.bifang.module.system.common.vo.person.PersonInfoSearchVO;
import com.bifang.module.system.common.vo.person.PersonInfoSelectVO;
import com.bifang.module.system.core.dao.model.PersonInfo;
import com.bifang.module.system.core.dao.service.PersonInfoService;
import com.jwsoft.manager.common.enums.EduStatusTypeEnum;
import com.jwsoft.manager.common.enums.EnrollPreTypeEnum;
import com.jwsoft.manager.common.enums.RelationTypeEnum;
import com.jwsoft.manager.common.enums.SaveTypeEnum;
import com.jwsoft.manager.common.vo.eduDataTaskDetail.DataTaskDetailOptionVO;
import com.jwsoft.manager.common.vo.eduStudentEnrollFamily.EduStudentEnrollFamilyVO;
import com.jwsoft.manager.common.vo.eduStudentEnrollPre.EduStudentEnrollPreVO;
import com.jwsoft.manager.common.vo.student.StudentInfoSearchVO;
import com.jwsoft.manager.common.vo.student.StudentInfoSelectVO;
import com.jwsoft.manager.core.dao.mapper.EduhStudentEnrollPreMapper;
import com.jwsoft.manager.core.dao.mapper.EdutStudentEnrollPreMapper;
import com.jwsoft.manager.core.dao.model.*;
import com.jwsoft.manager.core.dao.mapper.EduStudentEnrollPreMapper;
import com.jwsoft.manager.core.dao.service.EduDataTaskDetailService;
import com.jwsoft.manager.core.dao.service.EduStudentEnrollFamilyService;
import com.jwsoft.manager.core.dao.service.EduStudentEnrollPreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwsoft.manager.core.dao.service.TableService;
import com.jwsoft.manager.core.util.EduEnrollClearCacheHelper;
import com.jwsoft.manager.core.util.EduHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 预采集表服务实现类
 *
 * @author wangtao
 * @since 2022-11-25
 */
@Service
@Slf4j
public class EduStudentEnrollPreServiceImpl extends ServiceImpl<EduStudentEnrollPreMapper, EduStudentEnrollPre> implements EduStudentEnrollPreService, TableService {

    @Autowired
    private EduDataTaskDetailService detailService;

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Autowired
    private EduhStudentEnrollPreMapper hEnrollPreMapper;

    @Autowired
    private EdutStudentEnrollPreMapper tEnrollPreMapper;

    @Autowired
    PersonInfoService personInfoService;

    @Autowired
    private EduEnrollClearCacheHelper eduEnrollClearCacheHelper;

    @Autowired
    public EduStudentEnrollFamilyService eduStudentEnrollFamilyService;
    @Autowired
    private EduHelper eduHelper;

    //每次处理的数量为1w条数据
    private static final Integer PAGE_SIZE = 10000;

    @Override
    public List<StudentInfoSelectVO> searchPersonSelect(StudentInfoSearchVO vo) {
        return baseMapper.searchPersonSelect(vo);
    }


    @Override
    @Transactional
    public void saveSuccessExcel(EduStudentEnrollPreVO saveVO) {
        PersonInfo stuPersonInfo =
                this.savePerson(saveVO.getIdcard().toUpperCase(), saveVO.getCardType(), saveVO.getFullName());
        saveVO.setPersonId(stuPersonInfo.getPersonId());
        saveVO.setType(SaveTypeEnum.INSERT.getType());
        saveVO.setEnrollPreType(EnrollPreTypeEnum.ycj.getType());
        saveVO.setStudentEnrollPreId(IdUtil.simpleUUID());
        QueryWrapper<EduStudentEnrollPre> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(EduStudentEnrollPre::getStudentEnrollPreId).eq(EduStudentEnrollPre::getIdcard, saveVO.getIdcard())
                .eq(EduStudentEnrollPre::getYear, saveVO.getYear());
        EduStudentEnrollPre old = this.getOne(queryWrapper, false);
        if (!ObjectUtils.isEmpty(old)) {
            saveVO.setStudentEnrollPreId(old.getStudentEnrollPreId());
            saveFamily(saveVO, true);
        } else {
            saveFamily(saveVO, false);
        }
        saveVO.setAuthStatus(AuthStatusEnum.YES.getType());
        saveVO.setAuthRemarks("学校预采集导入审核通过");
        saveVO.setBeforeFlag("0");
        saveVO.setCalculateFlag("0");
        saveVO.setHouseholdSyncFlag("0");
        saveVO.setHouseSyncFlag("0");
        saveVO.setNetsignSyncFlag("0");
        saveVO.setCbSyncFlag("0");
        saveVO.setJzzSyncFlag("0");
        saveVO.setQySyncFlag("0");
        saveVO.setFamilyFlag("0");
        if(StringUtils.isBlank(saveVO.getConsistentType())){
            saveVO.setConsistentType("1");
        }
        saveVO.setConsistentType("1");
        EduStudentEnrollPre eduStudentEnrollPre = Convert.convert(EduStudentEnrollPre.class, saveVO);
        this.saveOrUpdate(eduStudentEnrollPre);
        eduEnrollClearCacheHelper.clearCacheMyEnrollPre(saveVO.getIdcard());
        eduEnrollClearCacheHelper.clearCacheMyEnrollPre(saveVO.getFatherIdcard());
        eduEnrollClearCacheHelper.clearCacheMyEnrollPre(saveVO.getMotherIdcard());
        eduEnrollClearCacheHelper.clearCachePreBefore(saveVO.getStudentEnrollPreId());
    }


    private void saveFamily(EduStudentEnrollPreVO saveVO, boolean uploadFlag) {
        if (uploadFlag) {
            eduStudentEnrollFamilyService.delByPreId(saveVO.getStudentEnrollPreId());
        }
        List<EduStudentEnrollFamily> familyList = new ArrayList<>();//新增
        if (!CollectionUtils.isEmpty(saveVO.getFamilyList())) {
            for (EduStudentEnrollFamilyVO eduStudentEnrollFamilyVO : saveVO.getFamilyList()) {
                if (RelationTypeEnum.mother.getType().equals(eduStudentEnrollFamilyVO.getRelationshipType())) {
                    PersonInfo moPersonInfo =
                            this.savePerson(saveVO.getMotherIdcard().toUpperCase(), saveVO.getMotherCardType(), saveVO.getMotherName());
                    saveVO.setMotherId(moPersonInfo.getPersonId());
                    saveVO.setMotherIdcard(saveVO.getMotherIdcard().toUpperCase());

                    EduStudentEnrollFamily family = new EduStudentEnrollFamily();
                    family.setStudentEnrollPreId(saveVO.getStudentEnrollPreId());
                    family.setCardType(eduStudentEnrollFamilyVO.getCardType());
                    family.setIdcard(eduStudentEnrollFamilyVO.getIdcard());
                    family.setPersonId(saveVO.getMotherId());
                    family.setFullName(eduStudentEnrollFamilyVO.getFullName());
                    family.setRelationshipType(RelationTypeEnum.mother.getType());
                    family.setHouseFlag(BoolEnum.TRUE.getType());
                    family.setCellphone(eduStudentEnrollFamilyVO.getCellphone());
                    family.setWork(eduStudentEnrollFamilyVO.getWork());
                    family.setFamilyFlag("0");
                    familyList.add(family);
                } else if (RelationTypeEnum.father.getType().equals(eduStudentEnrollFamilyVO.getRelationshipType())) {
                    PersonInfo faPersonInfo =
                            this.savePerson(saveVO.getFatherIdcard().toUpperCase(), saveVO.getFatherCardType(), saveVO.getFatherName());
                    saveVO.setFatherId(faPersonInfo.getPersonId());
                    saveVO.setFatherIdcard(saveVO.getFatherIdcard().toUpperCase());
                    EduStudentEnrollFamily family = new EduStudentEnrollFamily();
                    family.setStudentEnrollPreId(saveVO.getStudentEnrollPreId());
                    family.setCardType(eduStudentEnrollFamilyVO.getCardType());
                    family.setIdcard(eduStudentEnrollFamilyVO.getIdcard());
                    family.setPersonId(saveVO.getFatherId());
                    family.setFullName(eduStudentEnrollFamilyVO.getFullName());
                    family.setRelationshipType(RelationTypeEnum.father.getType());
                    family.setHouseFlag(BoolEnum.TRUE.getType());
                    family.setCellphone(eduStudentEnrollFamilyVO.getCellphone());
                    family.setWork(eduStudentEnrollFamilyVO.getWork());
                    family.setFamilyFlag("0");
                    familyList.add(family);
                } else {
                    PersonInfo personInfo =
                            this.savePerson(saveVO.getRelationshipIdcard().toUpperCase(), saveVO.getRelationshipCardType(), saveVO.getRelationshipName());
                    saveVO.setRelationshipIdcard(saveVO.getRelationshipIdcard().toUpperCase());
                    EduStudentEnrollFamily family = new EduStudentEnrollFamily();
                    family.setStudentEnrollPreId(saveVO.getStudentEnrollPreId());
                    family.setCardType(eduStudentEnrollFamilyVO.getCardType());
                    family.setIdcard(eduStudentEnrollFamilyVO.getIdcard());
                    family.setPersonId(personInfo.getPersonId());
                    family.setFullName(eduStudentEnrollFamilyVO.getFullName());
                    family.setRelationshipType(eduStudentEnrollFamilyVO.getRelationshipType());
                    family.setHouseFlag(BoolEnum.TRUE.getType());
                    family.setCellphone(eduStudentEnrollFamilyVO.getCellphone());
                    family.setWork(eduStudentEnrollFamilyVO.getWork());
                    family.setFamilyFlag("0");
                    familyList.add(family);
                }
            }
        } else {
            //管理端取消 监护人信息栏 ，合并到基本信息里，所以familyList 为空了
            //else if(EnrollPreTypeEnum.dj.getType().equals(saveVO.getEnrollPreType())||EnrollPreTypeEnum.ycj.getType().equals(saveVO.getEnrollPreType())){
            if (StringUtils.isNotBlank(saveVO.getMotherIdcard())) {
                PersonInfo moPersonInfo =
                        this.savePerson(saveVO.getMotherIdcard().toUpperCase(), saveVO.getMotherCardType(), saveVO.getMotherName());
                saveVO.setMotherId(moPersonInfo.getPersonId());
                saveVO.setMotherIdcard(saveVO.getMotherIdcard().toUpperCase());
                EduStudentEnrollFamily family = new EduStudentEnrollFamily();
                family.setStudentEnrollPreId(saveVO.getStudentEnrollPreId());
                family.setCardType(saveVO.getMotherCardType());
                family.setIdcard(saveVO.getMotherIdcard());
                family.setPersonId(saveVO.getMotherId());
                family.setFullName(saveVO.getMotherName());
                family.setRelationshipType(RelationTypeEnum.mother.getType());
                family.setHouseFlag(BoolEnum.TRUE.getType());
                family.setCellphone(saveVO.getMotherCellphone());
                family.setWork(saveVO.getMotherWork());
                family.setFamilyFlag("0");
                familyList.add(family);
            }
            if (StringUtils.isNotBlank(saveVO.getFatherIdcard())) {
                PersonInfo faPersonInfo =
                        this.savePerson(saveVO.getFatherIdcard().toUpperCase(), saveVO.getFatherCardType(), saveVO.getFatherName());
                saveVO.setFatherId(faPersonInfo.getPersonId());
                saveVO.setFatherIdcard(saveVO.getFatherIdcard().toUpperCase());
                EduStudentEnrollFamily family = new EduStudentEnrollFamily();
                family.setStudentEnrollPreId(saveVO.getStudentEnrollPreId());
                family.setCardType(saveVO.getFatherCardType());
                family.setIdcard(saveVO.getFatherIdcard());
                family.setPersonId(saveVO.getFatherId());
                family.setFullName(saveVO.getFatherName());
                family.setRelationshipType(RelationTypeEnum.father.getType());
                family.setHouseFlag(BoolEnum.TRUE.getType());
                family.setCellphone(saveVO.getFatherCellphone());
                family.setWork(saveVO.getFatherWork());
                family.setFamilyFlag("0");
                familyList.add(family);
            }
            if (StringUtils.isNotBlank(saveVO.getRelationshipIdcard())) {
                PersonInfo personInfo =
                        this.savePerson(saveVO.getRelationshipIdcard().toUpperCase(), saveVO.getRelationshipCardType(), saveVO.getRelationshipName());
                saveVO.setRelationshipIdcard(saveVO.getRelationshipIdcard().toUpperCase());
                EduStudentEnrollFamily family = new EduStudentEnrollFamily();
                family.setStudentEnrollPreId(saveVO.getStudentEnrollPreId());
                family.setCardType(saveVO.getRelationshipCardType());
                family.setIdcard(saveVO.getRelationshipIdcard());
                family.setPersonId(personInfo.getPersonId());
                family.setFullName(saveVO.getRelationshipName());
                family.setRelationshipType(saveVO.getRelationshipType());
                family.setHouseFlag(BoolEnum.TRUE.getType());
                family.setCellphone(saveVO.getCellphone());
                family.setWork(saveVO.getWork());
                family.setFamilyFlag("0");
                familyList.add(family);
            }
        }
        eduStudentEnrollFamilyService.saveBatch(familyList);
    }

    private PersonInfo savePerson(String idcard, String cardType, String fullName) {
        if (StringUtils.isBlank(cardType)) {
            cardType = CardTypeEnum.idcard.getType();
        }
        if(!CardTypeEnum.idcard.getType().equalsIgnoreCase(cardType)){
            if(IdcardUtil.isValidCard(idcard)){
                cardType= CardTypeEnum.idcard.getType();
            }
        }
        PersonInfo personInfo =  personInfoService.selectByCardTypeAndIdcard(cardType, StringUtils.upperCase(idcard));
        if (personInfo == null) {
            QueryWrapper<PersonInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(PersonInfo::getIdcard, idcard);
            List<PersonInfo> list = personInfoService.list(queryWrapper);
            if (!CollectionUtils.isEmpty(list)) {
                personInfo = list.get(0);
            }
        }
        if (ObjectUtils.isEmpty(personInfo)) {
            personInfo = new PersonInfo();
            personInfo.setIdcard(StringUtils.upperCase(idcard));
            personInfo.setFullName(fullName);
            personInfo.setCardType(cardType);
            return personInfoService.addPerson(personInfo);
        } else {
            if (!personInfo.getFullName().equals(fullName)||!personInfo.getCardType().equals(cardType)) {
                personInfo.setIdcard(StringUtils.upperCase(idcard));
                personInfo.setFullName(fullName);
                personInfo.setCardType(cardType);
                personInfoService.updateById(personInfo);
            }
            return personInfo;
        }
    }


    @Override
    public Integer dataMigration(DataTaskDetailOptionVO optionVO) {
        EduDataTaskDetail taskDetail = detailService.getById(optionVO.getTaskDetailId());
        log.info("调用了EduStudentEnrollPreServiceImpl服务的dataMigration()来进行处理迁移数据");
        Integer count = this.baseMapper.selectCountByYear(optionVO.getYear());
        log.info("edu_student_enroll_pre迁移" + count + "条数据");
        // 计算总页数
        int totalPages = (count % PAGE_SIZE == 0) ? (count / PAGE_SIZE) : (count / PAGE_SIZE + 1);
        // 计算循环次数 = 总页数
        for (int i = 0; i < totalPages; i++) {
            List<EduStudentEnrollPre> eduStudentEnrollPres =
                    this.baseMapper.selectByYear(optionVO.getYear(), PAGE_SIZE);
            if (eduStudentEnrollPres.size() > 0) {
                List<EduhStudentEnrollPre> hStudentEnrollPres = eduStudentEnrollPres.stream().map(item -> {
                    EduhStudentEnrollPre hEnrollPre = new EduhStudentEnrollPre();
                    BeanUtils.copyProperties(item, hEnrollPre);
                    hEnrollPre.setTaskDetailId(taskDetail.getTaskDetailId());
                    hEnrollPre.setEncryptFlag(BoolEnum.FALSE.getType());
                    hEnrollPre.setEncryptStatus(EduStatusTypeEnum.dcl.getType());
                    hEnrollPre.setCleanFlag(BoolEnum.FALSE.getType());
                    return hEnrollPre;
                }).collect(Collectors.toList());
                try {
                    this.performDataMigration(hStudentEnrollPres, eduStudentEnrollPres.size(), optionVO);
                } catch (Exception e) {
                    log.error("edu_student_enroll_pre迁移数据发生异常");
                    throw new RuntimeException("EduStudentEnrollPreServiceImpl:" + e);
                }
            }
        }
        return count;
    }

    private void performDataMigration(List<EduhStudentEnrollPre> hStudentEnrollPres, Integer count, DataTaskDetailOptionVO optionVO) {
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                try {
                    //1.数据迁移到历史表
                    int saveHCount = hEnrollPreMapper.saveBatch(hStudentEnrollPres);
                    if (saveHCount != count) {
                        throw new RuntimeException("数据迁移到历史表失败");
                    }
                    //2.数据销毁
                    EduDataTaskDetail taskDetail = detailService.getById(optionVO.getTaskDetailId());
                    if (ObjectUtils.isEmpty(taskDetail)) {
                        throw new AppException("任务数据不存在，请检查数据是否有误");
                    }
                    Integer deleteCount = baseMapper.deleteByTaskHistory(taskDetail.getTaskDetailId());
                    if (!count.equals(deleteCount)) {
                        throw new RuntimeException("数据删除失败");
                    }
                    log.info("edu_student_enroll_pre数据销毁成功！销毁数量：" + count);
                } catch (Exception e) {
                    status.setRollbackOnly();
                    throw e;
                }
            }
        });
    }

    @Override
    public void dataEncryption(DataTaskDetailOptionVO optionVO) {
        EduDataTaskDetail taskDetail = detailService.getById(optionVO.getTaskDetailId());
        log.info("调用了EduStudentEnrollPreServiceImpl服务的dataEncryption()来进行处理数据加密");
        Integer count = 0 ;
        if (taskDetail.getEncryptCount() == 0) {
            count = this.baseMapper.selectEncryptionCountByYear(optionVO.getYear(), optionVO.getTaskDetailId());
            taskDetail.setEncryptCount(count);
        }
        Boolean errorFlag = false;
        Integer errorCount = 0;
        // 计算总页数
        int totalPages = (count % PAGE_SIZE == 0) ? (count / PAGE_SIZE) : (count / PAGE_SIZE + 1);
        // 计算循环次数 = 总页数
        for (int i = 0; i < totalPages; i++) {
            List<EduhStudentEnrollPre> hStudentEnrollPres =
                    this.baseMapper.selectEncryptionByYear(optionVO.getYear(), optionVO.getTaskDetailId(),PAGE_SIZE);
            for (EduhStudentEnrollPre hStudentEnrollPre : hStudentEnrollPres) {
                try {
                    this.encrypt(hStudentEnrollPre);
                    hStudentEnrollPre.setEncryptTime(new Date());
                    hStudentEnrollPre.setEncryptStatus(EduStatusTypeEnum.success.getType());
                    hStudentEnrollPre.setEncryptRemarks("加密成功");
                    hStudentEnrollPre.setEncryptFlag(BoolEnum.TRUE.getType());
                    hEnrollPreMapper.updateById(hStudentEnrollPre);
                }catch (Exception e){
                    this.decrypt(hStudentEnrollPre);
                    hStudentEnrollPre.setEncryptTime(new Date());
                    hStudentEnrollPre.setEncryptStatus(EduStatusTypeEnum.error.getType());
                    hStudentEnrollPre.setEncryptFlag(BoolEnum.FALSE.getType());
                    hStudentEnrollPre.setEncryptRemarks("加密失败"+e.getMessage());
                    hEnrollPreMapper.updateById(hStudentEnrollPre);
                    errorFlag = true;
                    errorCount++;
                    throw new AppException(e);
                }
            }
        }
        if (!errorFlag) {
            //无异常，返回任务状态
            taskDetail.setEncryptStatus(EduStatusTypeEnum.success.getType());
            taskDetail.setEncryptRemarks("加密成功");
            taskDetail.setEncryptTime(new Date());
        } else {
            taskDetail.setEncryptStatus(EduStatusTypeEnum.error.getType());
            taskDetail.setEncryptTime(new Date());
            taskDetail.setEncryptRemarks(taskDetail.getHandleTable()
                    +"加密失败,加密出错的数据：" + errorCount);
        }
        detailService.updateById(taskDetail);
    }

    public void decrypt(EduhStudentEnrollPre hStudentEnrollPre) {
        if (StringUtils.isNotBlank(hStudentEnrollPre.getIdcard())){
            hStudentEnrollPre.setIdcard(CommonSecureUtil.sm4Decrypt(hStudentEnrollPre.getIdcard()));
        }
        if (StringUtils.isNotBlank(hStudentEnrollPre.getFullName())){
            hStudentEnrollPre.setFullName(CommonSecureUtil.sm4Decrypt(hStudentEnrollPre.getFullName()));
        }
        if (StringUtils.isNotBlank(hStudentEnrollPre.getFatherName())){
            hStudentEnrollPre.setFatherName(CommonSecureUtil.sm4Decrypt(hStudentEnrollPre.getFatherName()));
        }
        if (StringUtils.isNotBlank(hStudentEnrollPre.getFatherIdcard())){
            hStudentEnrollPre.setFatherIdcard(CommonSecureUtil.sm4Decrypt(hStudentEnrollPre.getFatherIdcard()));
        }
        if (StringUtils.isNotBlank(hStudentEnrollPre.getFatherCellphone())){
            hStudentEnrollPre.setFatherCellphone(CommonSecureUtil.sm4Decrypt(hStudentEnrollPre.getFatherCellphone()));
        }
        if (StringUtils.isNotBlank(hStudentEnrollPre.getMotherName())){
            hStudentEnrollPre.setMotherName(CommonSecureUtil.sm4Decrypt(hStudentEnrollPre.getMotherName()));
        }
        if (StringUtils.isNotBlank(hStudentEnrollPre.getMotherIdcard())){
            hStudentEnrollPre.setMotherIdcard(CommonSecureUtil.sm4Decrypt(hStudentEnrollPre.getMotherIdcard()));
        }
        if (StringUtils.isNotBlank(hStudentEnrollPre.getMotherCellphone())){
            hStudentEnrollPre.setMotherCellphone(CommonSecureUtil.sm4Decrypt(hStudentEnrollPre.getMotherCellphone()));
        }
    }

    @Override
    public void resetHjSyncStatus(Integer year, String areaCode) {
        List<String> areaCodeList = new ArrayList<>();
        if (year==null){
            year=eduHelper.thisYear();
        }
        if (StringUtils.isBlank(areaCode)){
            areaCode = SessionUtil.getAreaCode();
            if (StringUtils.isBlank(areaCode)) {
                areaCode = eduHelper.thisAreaCode();
            }
            if ("330700".equals(areaCode)) {
                areaCodeList.add("330700");
                areaCodeList.add("330701");
                areaCodeList.add("330702");
                areaCodeList.add("330703");
                areaCodeList.add("330704");
            }else{
                areaCodeList.add(areaCode);
            }
        }else{
            areaCodeList.add(areaCode);
        }
        this.baseMapper.resetHjSyncStatus(year,areaCodeList);
    }

    @Override
    public void resetFcSyncStatus(Integer year, String areaCode) {
        List<String> areaCodeList = new ArrayList<>();
        if (year==null){
            year=eduHelper.thisYear();
        }
        if (StringUtils.isBlank(areaCode)){
            areaCode = SessionUtil.getAreaCode();
            if (StringUtils.isBlank(areaCode)) {
                areaCode = eduHelper.thisAreaCode();
            }
            if ("330700".equals(areaCode)) {
                areaCodeList.add("330700");
                areaCodeList.add("330701");
                areaCodeList.add("330702");
                areaCodeList.add("330703");
                areaCodeList.add("330704");
            }else{
                areaCodeList.add(areaCode);
            }
        }else{
            areaCodeList.add(areaCode);
        }
        this.baseMapper.resetFcSyncStatus(year,areaCodeList);
    }

    @Override
    public void resetCbSyncStatus(Integer year, String areaCode) {
        List<String> areaCodeList = new ArrayList<>();
        if (year==null){
            year=eduHelper.thisYear();
        }
        if (StringUtils.isBlank(areaCode)){
            areaCode = SessionUtil.getAreaCode();
            if (StringUtils.isBlank(areaCode)) {
                areaCode = eduHelper.thisAreaCode();
            }
            if ("330700".equals(areaCode)) {
                areaCodeList.add("330700");
                areaCodeList.add("330701");
                areaCodeList.add("330702");
                areaCodeList.add("330703");
                areaCodeList.add("330704");
            }else{
                areaCodeList.add(areaCode);
            }
        }else{
            areaCodeList.add(areaCode);
        }
        this.baseMapper.resetCbSyncStatus(year,areaCodeList);
    }

    @Override
    public void resetJzzSyncStatus(Integer year, String areaCode) {
        List<String> areaCodeList = new ArrayList<>();
        if (year==null){
            year=eduHelper.thisYear();
        }
        if (StringUtils.isBlank(areaCode)){
            areaCode = SessionUtil.getAreaCode();
            if (StringUtils.isBlank(areaCode)) {
                areaCode = eduHelper.thisAreaCode();
            }
            if ("330700".equals(areaCode)) {
                areaCodeList.add("330700");
                areaCodeList.add("330701");
                areaCodeList.add("330702");
                areaCodeList.add("330703");
                areaCodeList.add("330704");
            }else{
                areaCodeList.add(areaCode);
            }
        }else{
            areaCodeList.add(areaCode);
        }
        this.baseMapper.resetJzzSyncStatus(year,areaCodeList);
    }

    @Override
    public void resetBeforeFlag(Integer year, String schoolCategory, String areaCode) {
        List<String> areaCodeList = new ArrayList<>();
        if (year==null){
            year=eduHelper.thisYear();
        }
        if (StringUtils.isBlank(areaCode)){
            areaCode = SessionUtil.getAreaCode();
            if (StringUtils.isBlank(areaCode)) {
                areaCode = eduHelper.thisAreaCode();
            }
            if ("330700".equals(areaCode)) {
                areaCodeList.add("330700");
                areaCodeList.add("330701");
                areaCodeList.add("330702");
                areaCodeList.add("330703");
                areaCodeList.add("330704");
            }else{
                areaCodeList.add(areaCode);
            }
        }else{
            areaCodeList.add(areaCode);
        }
        this.baseMapper.resetBeforeFlag(year,schoolCategory,areaCodeList);
    }

    private void encrypt(EduhStudentEnrollPre hStudentEnrollPre) {
        if (StringUtils.isNotBlank(hStudentEnrollPre.getIdcard())){
            hStudentEnrollPre.setIdcard(CommonSecureUtil.sm4Encrypt(hStudentEnrollPre.getIdcard()));
        }
        if (StringUtils.isNotBlank(hStudentEnrollPre.getFullName())){
            hStudentEnrollPre.setFullName(CommonSecureUtil.sm4Encrypt(hStudentEnrollPre.getFullName()));
        }
        if (StringUtils.isNotBlank(hStudentEnrollPre.getFatherName())){
            hStudentEnrollPre.setFatherName(CommonSecureUtil.sm4Encrypt(hStudentEnrollPre.getFatherName()));
        }
        if (StringUtils.isNotBlank(hStudentEnrollPre.getFatherIdcard())){
            hStudentEnrollPre.setFatherIdcard(CommonSecureUtil.sm4Encrypt(hStudentEnrollPre.getFatherIdcard()));
        }
        if (StringUtils.isNotBlank(hStudentEnrollPre.getFatherCellphone())){
            hStudentEnrollPre.setFatherCellphone(CommonSecureUtil.sm4Encrypt(hStudentEnrollPre.getFatherCellphone()));
        }
        if (StringUtils.isNotBlank(hStudentEnrollPre.getMotherName())){
            hStudentEnrollPre.setMotherName(CommonSecureUtil.sm4Encrypt(hStudentEnrollPre.getMotherName()));
        }
        if (StringUtils.isNotBlank(hStudentEnrollPre.getMotherIdcard())){
            hStudentEnrollPre.setMotherIdcard(CommonSecureUtil.sm4Encrypt(hStudentEnrollPre.getMotherIdcard()));
        }
        if (StringUtils.isNotBlank(hStudentEnrollPre.getMotherCellphone())){
            hStudentEnrollPre.setMotherCellphone(CommonSecureUtil.sm4Encrypt(hStudentEnrollPre.getMotherCellphone()));
        }
    }

    @Override
    public Integer dataDestruction(DataTaskDetailOptionVO optionVO) {
        return 0;
    }

    @Override
    public void tempDataDestruction() {

    }

    @Override
    public void dataDecrypt(DataTaskDetailOptionVO optionVO) {
        Integer count = Math.toIntExact(hEnrollPreMapper.selectCount(new QueryWrapper<EduhStudentEnrollPre>().lambda().eq(EduhStudentEnrollPre::getTaskDetailId, optionVO.getTaskDetailId())));
        // 计算总页数
        int totalPages = (count % PAGE_SIZE == 0) ? (count / PAGE_SIZE) : (count / PAGE_SIZE + 1);
        int currentPage = 0;
        // 计算循环次数 = 总页数
        for (int i = 0; i < totalPages; i++) {
            List<EduhStudentEnrollPre> hEnrollPres =
                    hEnrollPreMapper.selectByTaskDetailId(optionVO.getTaskDetailId(),currentPage,PAGE_SIZE);
            List<EdutStudentEnrollPre> tEnrollPres = hEnrollPres.stream().map(item -> {
                if (BoolEnum.TRUE.getType().equalsIgnoreCase(item.getEncryptFlag())) {
                    //需要进行解密
                    this.decrypt(item);
                }
                EdutStudentEnrollPre tEnrollPre = new EdutStudentEnrollPre();
                BeanUtils.copyProperties(item, tEnrollPre);
                return tEnrollPre;
            }).collect(Collectors.toList());
            tEnrollPreMapper.saveBatch(tEnrollPres);
            currentPage ++;
        }

    }
}
